//
//  YuchBoxAppDelegate.m
//  YuchBox
//
//  Created by Ivan on 11-9-28.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "YuchBoxAppDelegate.h"
#import "LayoutParam.h"

@interface YuchBoxAppDelegate (private)
- (void)setupMainView;
@end

@implementation YuchBoxAppDelegate

@synthesize window                      = _window;
@synthesize springboardController       = _springboardController;
@synthesize rootToolBarViewController   = _rootToolBarViewController;
@synthesize navigationCotrollernew      = _navigationCotrollernew;
@synthesize stateViewer                 = _stateViewer;

@synthesize accountSession              = _accountSession;

+ (YuchBoxAppDelegate *)shareAppDelegate
{
    return (YuchBoxAppDelegate *)[[UIApplication sharedApplication] delegate];
}

+ (StateViewer *)shareStateViewer
{
    return ((YuchBoxAppDelegate *)[[UIApplication sharedApplication] delegate]).stateViewer;
}

+ (Account *)shareAccount
{
    return ((YuchBoxAppDelegate *)[[UIApplication sharedApplication] delegate]).accountSession;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    [[NSThread currentThread] setName:@"main"];
    // Override point for customization after application launch.

    [self setupSpringboardView];
    [self setupMainView];
    [self.window makeKeyAndVisible];
    
    _accountSession = [[Account alloc] init];
    return YES;
}

- (void)dealloc {
    [_window release];
    [_accountSession release];
    [_springboardController release];
    [_rootToolBarViewController release];

    [super dealloc];
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
    NSLog(@"will ResignActive\n");
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
    //sleep(6);
//    UIDevice* device = [UIDevice currentDevice];  
//    BOOL backgroundSupported = NO;  
//    if ([device respondsToSelector:@selector(isMultitaskingSupported)])  {
//        backgroundSupported = device.multitaskingSupported; 
//    }
//    NSLog(@"will EnterBackgroud\n");
    [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{
        TTLOG_DEBUG(@"Send keep alive message in the background.");
        [DATA_SOURCE sendKeepAlive];
    }];
    
//    NSLog(@"%d", ret);
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
    NSLog(@"will EnterForegroud\n");
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
    NSLog(@"will BecomeActive\n");
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    NSLog(@"will Terminate\n");
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

- (void)setSubViewTop:(WindowViewTag)viewTag
{
    UIView *topView = [self.window viewWithTag:viewTag];
    if (nil != topView) {
        [self.window bringSubviewToFront:topView];
    }
}

- (void)setupSpringboardView
{
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    SpringboardController *springboardController = [[SpringboardController alloc] initWithNibName:@"SpringboardController" bundle:nil];
    [self setSpringboardController:springboardController];
    [springboardController release];
    
    [self.springboardController.view setTag:WindowViewTagForSpringboard];
    [self.window addSubview:self.springboardController.view];
//    [self.window setRootViewController:self.springboardController];
}

- (void)setupMainView
{
    if (self.navigationCotrollernew != nil) {
        return;
    }
    
    StateViewer *stateViewer = [[StateViewer alloc] initWithOrigin:CGPointMake(110, 24)];
    [self setStateViewer:stateViewer];
//    [self.stateViewer setTag:WindowViewTagForStateViewer];
    [stateViewer release];
    
    MTRootToolBarViewController *rootToolBarViewController = [[MTRootToolBarViewController alloc] init];
    [self setRootToolBarViewController:rootToolBarViewController];
    [rootToolBarViewController release];
    
    // TODO put this rootViewInit in the backgroud thread
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootToolBarViewController];
    [self setNavigationCotrollernew:navigationController];
    
    [navigationController release];
    [self.navigationCotrollernew.view setTag:WindowViewTagForNavigationBar];

    [UIView beginAnimations:@"animationID" context:nil];
    [UIView setAnimationDelay:0.6f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.window cache:YES];
    [self.window addSubview:self.navigationCotrollernew.view];
    [UIView commitAnimations];
    
    [self.navigationCotrollernew.view addSubview:self.stateViewer];
}

#pragma mark -
#pragma DataSourceDelegate

- (void)callBackHandle:(DataSourceEvent)eventCode info:(id)info
{
    [_springboardController eventInvoke:eventCode info:info];
    
    MTEmailViewController *emailViewController = (MTEmailViewController *)[_rootToolBarViewController.ivanTabBar viewControllerAtIndex:0];
    [emailViewController eventInvoke:eventCode info:info];
}

@end
